package com.example.administrator.swredeem.net;

import android.util.Log;

import com.example.administrator.swredeem.appBase.Config;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class HttpXutils {


    public static void getData(String url, Map<String, String> map, MyCallback myCallback) {

        RequestParams params = new RequestParams(url);
        params.setLoadingUpdateMaxTimeSpan(15*1000);
        // 获取所有键值对对象的集合
        Set<Map.Entry<String, String>> set = map.entrySet();
        for (Map.Entry<String, String> me : set) {
            // 根据键值对对象获取键和值
            String key = me.getKey().trim();
            String value = me.getValue().trim();
//            Log.d("TAG", "getData: " + key + "---" + value);
            params.addQueryStringParameter(key, value);
        }
        List list = params.getQueryStringParams();
        for (int i = 0; i < list.size(); i++) {
            Log.d("TAG", "getData: " +list.get(i));
        }
        Log.i("dylan","请求链接："+params.toString());
        x.http().get(params, myCallback);

    }

    public static void getPost(String url, Map<String, String> map, MyCallback myCallback) {

        RequestParams params = new RequestParams(url);

        // 获取所有键值对对象的集合
        Set<Map.Entry<String, String>> set = map.entrySet();
        for (Map.Entry<String, String> me : set) {
            // 根据键值对对象获取键和值
            String key = me.getKey().trim();
            String value = me.getValue().trim();
//            Log.d("TAG", "getData: " + key + "---" + value);
            params.addBodyParameter(key, value);
        }
        List list = params.getQueryStringParams();
        for (int i = 0; i < list.size(); i++) {
            Log.d("TAG", "getData: " +list.get(i));
        }
        Log.i("dylan","请求链接："+params.toString());
        x.http().post(params, myCallback);

    }





    /**
     * 将Map中的数据转换成key1=value1&key2=value2的形式
     *
     * @param data 待拼接的Map数据
     * @return 拼接好后的字符串
     */
    public static String coverMap2String(Map<String, String> data) {
        TreeMap<String, String> tree = new TreeMap<String, String>();
        Iterator<Map.Entry<String, String>> it = data.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> en = it.next();
//			if ("signature".equals(en.getKey().trim())) {
//				continue;
//			}
            tree.put(en.getKey(), en.getValue());
        }
        it = tree.entrySet().iterator();
        StringBuffer sf = new StringBuffer();
        while (it.hasNext()) {
            Map.Entry<String, String> en = it.next();
            sf.append(en.getKey() + "=" + en.getValue()
                    + "&");
        }
        return sf.substring(0, sf.length() - 1);
    }


}
